﻿
#include "Common.h"
#include "GUI.h"
#include "DIALOG.h"
#include <string.h>

#if !defined(DEBUG_IN_PC)
#include "aworks.h"
#include "aw_pwm.h"
#include "aw_delay.h"
#include "aw_gpio.h"
#include "aw_led.h"
#include <stdlib.h>
#include "aw_vdebug.h"
#else
#include <Windows.h>
#endif





#define GPIO_KEY_REST (31)
#define GPIO_KEY_LED (28)




static const uint8_t widgetType[WIDGET_NUMBER][20] = { "button",
"static text",
"slide" };

static int16_t uniqueID = GUI_ID_USER + 1;
static uint8_t editMode = 0;

static uint8_t LongKey, ShortKey, KeyFlag;




void SetEditMode(uint8_t mode)
{
	editMode = mode;
}


uint8_t GetEditMode(void)
{
	return editMode;
}


int16_t GetUniqueID(void)
{
	int16_t retID = 0;
	
	if (uniqueID < 32767)
		retID = uniqueID++;
	
	return retID;
}


uint8_t *GetWidetTypeName(uint16_t winType)
{
	static uint8_t NullName[] = "";
	
	if (winType < WIDGET_NUMBER)
		return (uint8_t *)widgetType[winType];
	else
		return NullName;
}


void *GetWidgetCreateFunction(uint16_t winType)
{
	void *pFunc = NULL;
	
	switch (winType)
	{
	case WIDGET_WINDOW:
		pFunc = WINDOW_CreateIndirect;
		break;
	case WIDGET_BUTTON:
	case WIDGET_BUTTON_SLIDE:
		pFunc = BUTTON_CreateIndirect;
		break;
	case WIDGET_STATIC_TEXT:
		pFunc = TEXT_CreateIndirect;
		break;
	case WIDGET_SLIDE:
		pFunc = SLIDER_CreateIndirect;
		break;
	case WIDGET_PROGBAR:
		pFunc = PROGBAR_CreateIndirect;
		break;
	case WIDGET_LIST_WHEEL:
		pFunc = LISTWHEEL_CreateIndirect;
		break;
    case WIDGET_CHECKBOX:
		pFunc = CHECKBOX_CreateIndirect;
		break;
	case WIDGET_EDIT:
		pFunc = EDIT_CreateIndirect;
		break;
	case WIDGET_DROPDOWN:
		pFunc = DROPDOWN_CreateIndirect;
		break;
	default:
		break;
	}
	
	return pFunc;
}


void SetWidgetText(pPARA_TCB pParaTcb, uint8_t *pStr)
{
	void *pFunc = NULL;
	
	switch (pParaTcb->guiTCB.winType)
	{
	case WIDGET_WINDOW:
		BUTTON_SetText(WM_GetDialogItem(pParaTcb->parent, pParaTcb->guiTCB.id), pStr);
		break;
	case WIDGET_BUTTON:
		BUTTON_SetText(WM_GetDialogItem(pParaTcb->parent, pParaTcb->guiTCB.id), pStr);
		break;
	case WIDGET_STATIC_TEXT:
		TEXT_SetText(WM_GetDialogItem(pParaTcb->parent, pParaTcb->guiTCB.id), pStr);
		break;
	case WIDGET_DROPDOWN:
		break;
	default:
		break;
	}
}


uint16_t GetWidgetNumber(pPARA_TCB pParaTcb)
{
	uint16_t count = 0;
	
	while (1)
	{
		if (pParaTcb->pParallelNext != NULL)
			count++;
		
		pParaTcb = pParaTcb->pParallelNext;
		if (pParaTcb == NULL)
		{
			count++;
			break;
		}
	}
	
	return count;
}


#if !defined(DEBUG_IN_PC)

#define AW_PWM_ID              2
/**
* \brief PWM演示例程入口
* \return 无
*/
void demo_pwm_back_light_entry (uint8_t DutyCycle)
 
{
 
  aw_pwm_config(AW_PWM_ID, DutyCycle*10000, 1000000);    // 配置 PWM 的有效时间（高电平时间） ,周期 100
                 
    aw_pwm_enable(AW_PWM_ID);      /* 使能通道 */
    aw_mdelay(250);
 
}



aw_local void __key_reset_entry(void *p_arg)
{
	
	uint_t val;
	KeyFlag = 0;
	
	/* GPIO触发初始化 */
	
	int gpio_key[] = { GPIO_KEY_REST | GPIO_KEY_LED };
	if (aw_gpio_pin_request("gpio_key",
							gpio_key,
							AW_NELEMENTS(gpio_key)) == AW_OK) {
								
								/* LCD-REST-GPIO 引脚配置 */
								aw_gpio_pin_cfg(GPIO_KEY_REST, AW_GPIO_INPUT);
								aw_gpio_pin_cfg(GPIO_KEY_LED, AW_GPIO_OUTPUT | AW_GPIO_PUSH_PULL);
								
							}
	
	
	aw_mdelay(500);
	
	AW_FOREVER{
		
		aw_mdelay(1000);
		
		uint16_t j,h,k;
		
		LongKey = 0;
		ShortKey = 0;
		
		val = aw_gpio_get(GPIO_KEY_REST);
		if (val == 0 && KeyFlag == 0)  //键按下去的时候，标志位用于防止长短按键二次判定
		{
			for (j = 0; j < 3000; j++);  //防抖
			if (val == 0)
			{
				h = 0;
				k = 0;
				KeyFlag = 1;           //计时初始值 
				while (1)        //看按了多久 
				{
					val = aw_gpio_get(GPIO_KEY_REST);
					aw_mdelay(10);     //调用延时10ms子程序
					h++;
					if (val == 0)k++;
					if (h >= 200)break;   //当按键时间过长(约2s)，则自动跳出
				}
				if (k == 200)LongKey = 1;  //表示为长按(约4s)
				else ShortKey = 1;      //表示为短按     
			}
		}
		else if (val == 1)          //按键弹起 进行下一次长短按判定
		{
			KeyFlag = 0;
		}
		
		if (LongKey == 1)
		{
			aw_gpio_set(GPIO_KEY_LED,0);           //USER code
			aw_mdelay(1000);
			aw_gpio_set(GPIO_KEY_LED,1);
			LongKey = 0;
		}
		if (ShortKey == 1)
		{
			int m;                                 //USER code
			for (m = 0; m <= 20; m++)
			{
				aw_mdelay(500);
				aw_gpio_set(GPIO_KEY_LED,0);
				aw_mdelay(500);
				aw_gpio_set(GPIO_KEY_LED,1);
				
			}
			ShortKey = 0;
		}
		
		
	}
	
}

void demo_gpio_key_reset_entry(void)
{
	/* 定义任务实体，分配栈空间大小为512  */
	AW_TASK_DECL_STATIC(gpio_key_reset_task, 512);
	
	AW_TASK_INIT(gpio_key_reset_task,    /* 任务实体 */
				 "gpio_key_reset",  /* 任务名字 */
				 4,                    /* 任务优先级 */
				 512,                  /* 任务堆栈大小 */
				 __key_reset_entry, /* 任务入口函数 */
				 NULL);                 /* 任务入口参数 */
	
	AW_TASK_STARTUP(gpio_key_reset_task);
}


void AddStringToBuffer(uint8_t *pInArray, uint8_t *pSrcString)
{
	strcat(pInArray, pSrcString);
}
#else

void AddStringToBuffer(uint8_t *pInArray, uint8_t *pSrcString)
{
	uint16_t number, *pUnicode, *pUtf8;
	
	number     = MultiByteToWideChar(CP_ACP, 0, pSrcString, -1, NULL, 0);
	pUnicode   = (uint16_t*)malloc(number * 2);
	number     = MultiByteToWideChar(CP_ACP, 0, pSrcString, -1, pUnicode, number);

	number     = WideCharToMultiByte(CP_UTF8, 0, pUnicode, -1, NULL, 0, NULL, NULL);
	pUtf8      = (uint16_t*)malloc(number * 3);
	WideCharToMultiByte(CP_UTF8, 0, pUnicode, -1, pUtf8, number, NULL, NULL);

	strcat(pInArray, pUtf8);

	free(pUnicode);
	free(pUtf8);
}
#endif
